// pages/mine/patrolRecord/patrolRecord.js
import { lgRequest } from '../../../utils/ajax'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sysInfo: getApp().globalData.sysInfo,
    isIphoneX: getApp().globalData.isIphoneX,
    statusBarHeight: getApp().globalData.statusBarHeight,
    titleBarHeight: getApp().globalData.titleBarHeight,
    userId:'',
    searchList:[],
    userInfo:wx.getStorageSync('userInfo'),
    positionImg:'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/15/9264ebd2b29d48393bc42933b76b8790.png',
    rightImgIcon:'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/06/24/a14ca79c67896c3fba0b575e86f3fe34.png',
    toHandleImg:'https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/15/d75401a8e782b01eebab544b39586b33.png',
    showTaskProcessing:false,
    option1: [
      { text: '全部商品', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 },
    ],
    array: ['正常', '异常'],
    index: 0,
    index: '',
    picStateInfoList:[],
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChangeC: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(e)
    this.setData({
      indexC: e.detail.value
    })
  },
  onClose(){
    this.setData({
      showTaskProcessing:false
    })
  },
  goBack(){
    wx.navigateBack({
      delta: 1
    })
  },
  // 获取经纬度
  getLocation(){
    wx.getLocation({
  
      success: (result) => {
        this.setData({
          latitude : result.latitude,
          longitude : result.longitude,
        })
        this.taskPatrolList()
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  
    // 计算距离
    Rad(d){
      return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
    },
    getDistance(lat1,lng1,lat2,lng2){
      var radLat1 = lat1* Math.PI / 180.0;
      var radLat2 = lat2* Math.PI / 180.0;
      var a = radLat1 - radLat2;
      var  b = lng1* Math.PI / 180.0 - lng2* Math.PI / 180.0;
      var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
      Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
      s = s *6378.137 ;// EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000; //输出为公里
      //s=s.toFixed(2);
      return s;
    },
  //任务巡查列表
  taskPatrolList(){
    let params = { 
      executor:this.data.userInfo.userId
    }
    lgRequest('taskPatrolList', params, {
      errAlert: false,
      doSuccess: r => {
        if (r.code == 200) {
          let taskPatrolList = r.data
          taskPatrolList.forEach(element => {
            let arr = []
            arr =element.gps.split(",");
            console.log("arr",arr)
            let a = this.getDistance(arr[1],arr[0],this.data.latitude,this.data.longitude)
            console.log(a)
            element.distance=a

          });
          this.setData({
            taskPatrolList
          })
        } else {
          wx.showToast({
            title: r.msg,
            icon: 'none'
          })
        }
      },
      doComplete: r => {
      }
    })
  },
  showTaskProcessing(e){
    console.log(e)
    let selectTask = e.currentTarget.dataset.item;
    // 超出50米不可操作
    // if(selectTask.distance>0.05){
    //   wx.showToast({
    //     title: '超出可操作范围',
    //     icon:'error'
    //   })
    //   return
    // }
    this.setData({
      showTaskProcessing:true,
      selectTask
    })
  },
  getPType(){
    let params = {
      code : "order_type"
    }
  lgRequest("dictionaries",params,{
    doSuccess:r=>{
      console.log(r)
      this.setData({
        PType:r.data
      })
    }
  })
  },
  afterRead(event) {
    const { file } = event.detail;
    let server = getApp().globalData.server
    let Authorization = getApp().globalData.Authorization
    console.log(file)
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: `${server}common/upload`, // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      // formData: { user: 'test' },
      header:{
        Authorization
      },
      success:(res)=> {
         let data = JSON.parse(res.data);
         console.log(data)
          let obj = {
            url: data.url,

          };
          let picStateInfoList = this.data.picStateInfoList;
          picStateInfoList.unshift(obj);
          this.setData({
            picStateInfoList,
          });
      },
    });
  },
  getTaskRemark(e){
    console.log(e)
    this.setData({
      TaskRemark:e.detail.value
    })
  },
  submitPatrol(){
    let indexC = this.data.indexC
    let params = {
      zhanId : this.data.selectTask.zhanId,
      patrolContent:this.data.selectTask.patrolContent,
      status : 1,
      rcTime:this.data.selectTask.rcTime,
      pid:this.data.selectTask.pid,
      wType:this.data.index==1?this.data.PType[indexC].valueName:'',
      result:this.data.index==1?"异常":"正常"

    }
    lgRequest("submitPatrol",params,{
      doSuccess:r=>{
        console.log(r)
        this.taskPatrolList()
        this.onClose()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getPType()
    this.getLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})